package com.youku.plugins.toolboxV4.view
{
   import flash.display.MovieClip;
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.plugins.toolboxV4.model.ToolboxDataProxy;
   import com.youku.plugins.toolboxV4.model.CloudDataProxy;
   import com.youku.events.PlayerControlEvent;
   import com.youku.events.ConductorEvent;
   import com.youku.events.CoreEvent;
   import com.youku.plugins.toolboxV4.ToolboxEvent;
   import com.youku.events.PluginEvent;
   import com.youku.plugins.toolboxV4.model.ToolboxData;
   import com.youku.plugins.toolboxV4.ToolboxConstant;
   import com.youku.data.PlayerConstant;
   import com.youku.interfaces.IDVDData;
   import com.youku.PlayerConfig;
   
   public class ToolBoxMediator extends Object
   {
      
      public function ToolBoxMediator(param1:MovieClip, param2:IPlayerProxy, param3:ToolboxDataProxy) {
         super();
         this._mainMc = param1;
         this._playerProxy = param2;
         this._dataProxy = param3;
         this._cloudDataProxy = new CloudDataProxy();
         this._parentWidth = PlayerConfig.normalWidth;
         this._parentHeight = PlayerConfig.normalHeight;
         this._isShowing = false;
         this._toolBox = new Toolbox();
         this.init();
      }
      
      private var _mainMc:MovieClip;
      
      private var _playerProxy:IPlayerProxy;
      
      private var _toolBox:Toolbox;
      
      private var _parentWidth:Number;
      
      private var _parentHeight:Number;
      
      private var _isShowing:Boolean;
      
      private var _dataProxy:ToolboxDataProxy;
      
      private var _cloudDataProxy:CloudDataProxy;
      
      protected function get toolbox() : Toolbox {
         return this._toolBox;
      }
      
      protected function get toolboxDataProxy() : ToolboxDataProxy {
         return this._dataProxy;
      }
      
      private function init() : void {
         this._playerProxy.addEventListener(PlayerControlEvent.SHOW_TOOLBOX,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_SET_CONTINUOUS,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_SET_JUMP,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.BEFORE_SHOW_PANELS,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.FULLSCREEN_CHANGED,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.PLAYER_SIZE_CHANGED,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_RESET,this.onPlayerEvent);
         this._playerProxy.addEventListener(ConductorEvent.METADATA_UPDATED,this.onConductorEvent);
         this._playerProxy.addEventListener(ConductorEvent.STATE_CHANGED,this.onConductorEvent);
         this._playerProxy.addEventListener(CoreEvent.QUALITY_CHANGED,this.onCoreEvent);
         this.toolbox.addEventListener(ToolboxEvent.CONFIRM_BUTTON_CLICK,this.onToolboxEvent);
         this.toolbox.addEventListener(ToolboxEvent.CLOSE_BUTTON_CLICK,this.onToolboxEvent);
         this.toolbox.addEventListener(ToolboxEvent.CANCLE_BUTTON_CLICK,this.onToolboxEvent);
         this.toolbox.addEventListener(ToolboxEvent.QUALITY_1080P_CLICK,this.onToolboxEvent);
         this._cloudDataProxy.addEventListener(ToolboxEvent.CLOUD_DATA_SET_SUCCESS,this.onToolboxEvent);
         this._cloudDataProxy.addEventListener(ToolboxEvent.CLOUD_DATA_SET_ERROR,this.onToolboxEvent);
      }
      
      private function onToolboxEvent(param1:ToolboxEvent) : void {
         var _loc2_:* = false;
         var _loc3_:* = false;
         switch(param1.type)
         {
            case ToolboxEvent.CONFIRM_BUTTON_CLICK:
               _loc2_ = this._cloudDataProxy.saveGlobalCloudData(this.toolbox.getCurrentData(),this.toolboxDataProxy.toolboxData);
               _loc3_ = this.saveCurrentState();
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.TOOLBOX_CONTINUOUS_CHANGE,{"continuous":this.toolboxDataProxy.toolboxData.isContinuous}));
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.TOOLBOX_JUMP_CHANGE,{"jump":this.toolboxDataProxy.toolboxData.isJump}));
               if(_loc3_)
               {
                  this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.TOOLBOX_DATA_CHANGED));
                  if(this.toolboxDataProxy.toolboxData.isVoiceUserChanged)
                  {
                     this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.TOOLBOX_VOICE_CHANGE,{"voice":this.toolboxDataProxy.toolboxData.voice}));
                  }
                  else if(this.toolboxDataProxy.toolboxData.isQualityUserChanged)
                  {
                     this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.TOOLBOX_QUALITY_CHANGE,{"quality":this.toolboxDataProxy.toolboxData.quality}));
                  }
                  
               }
               this.restoreState();
               this.closeToolbox();
               break;
            case ToolboxEvent.QUALITY_1080P_CLICK:
               this.check1080P();
               break;
            case ToolboxEvent.CLOSE_BUTTON_CLICK:
            case ToolboxEvent.CANCLE_BUTTON_CLICK:
               this.restoreState();
               this.closeToolbox();
               break;
            case ToolboxEvent.CLOUD_DATA_SET_SUCCESS:
               break;
            case ToolboxEvent.CLOUD_DATA_SET_ERROR:
               break;
         }
      }
      
      private function check1080P() : void {
         if(this._playerProxy.playerData.hd3Enabled == false)
         {
            this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.ALERT_BOX_SHOW,
               {
                  "type":"1080P",
                  "from":"toolbox"
               }));
            this.toolbox.setDefaultQuality(this.toolboxDataProxy.toolboxData.defaultQuality);
            this.toolbox.selectQuality(this.toolboxDataProxy.toolboxData.quality);
         }
      }
      
      private function closeToolbox() : void {
         this.hideToolbox();
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.TOOLBOX_CLOSE));
      }
      
      private function restoreState() : void {
         var _loc1_:ToolboxData = this.toolboxDataProxy.toolboxData;
         this.toolbox.setDefaultQuality(_loc1_.defaultQuality);
         this.toolbox.selectQuality(_loc1_.quality);
         this.toolbox.setFunctionValues(_loc1_.isContinuous,_loc1_.isJump);
         this.toolbox.setVoice(_loc1_.voice);
      }
      
      private function saveCurrentState() : Boolean {
         var _loc1_:ToolboxData = this.toolbox.getCurrentData();
         var _loc2_:Boolean = this.toolboxDataProxy.saveData(_loc1_);
         return _loc2_;
      }
      
      private function saveFunctionalData() : Boolean {
         var _loc1_:ToolboxData = this.toolbox.getCurrentData();
         var _loc2_:Boolean = this.toolboxDataProxy.saveFunctionalData(_loc1_);
         return _loc2_;
      }
      
      private function saveQualityData() : Boolean {
         var _loc1_:ToolboxData = this.toolbox.getCurrentData();
         var _loc2_:Boolean = this.toolboxDataProxy.saveQualityData(_loc1_);
         return _loc2_;
      }
      
      private function saveDVDData() : Boolean {
         var _loc1_:ToolboxData = this.toolbox.getCurrentData();
         var _loc2_:Boolean = this.toolboxDataProxy.saveDVDData(_loc1_);
         return _loc2_;
      }
      
      private function showToolbox() : void {
         if(!this._mainMc.contains(this.toolbox))
         {
            this._mainMc.addChild(this.toolbox);
         }
         this._isShowing = true;
         this.toolbox.visible = true;
         this.updateLayout();
         this.toolbox.showPanel();
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.PANEL_STATE_CHANGE,
            {
               "type":"toolbox",
               "state":true
            }));
      }
      
      private function hideToolbox() : void {
         if(this._isShowing)
         {
            if(this._mainMc.contains(this.toolbox))
            {
               this._mainMc.removeChild(this.toolbox);
            }
            this.toolbox.visible = false;
            this._isShowing = false;
            this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.PANEL_STATE_CHANGE,
               {
                  "type":"toolbox",
                  "state":false
               }));
         }
      }
      
      private function updateLayout() : void {
         this.toolbox.x = Math.floor((this._parentWidth - this.toolbox.width * this.toolbox.scaleX) / 2);
         this.toolbox.y = Math.floor((this._parentHeight - this.toolbox.height * this.toolbox.scaleY) / 2);
      }
      
      private function updateSize() : void {
         this.toolbox.setSize(ToolboxConstant.TOOLBOX_WIDTH,ToolboxConstant.TOOLBOX_HEIGHT);
         var _loc1_:Number = 1;
         var _loc2_:Number = 1;
         if(this._parentWidth < ToolboxConstant.TOOLBOX_WIDTH || this._parentHeight < ToolboxConstant.TOOLBOX_HEIGHT)
         {
            if(this._parentWidth / this._parentHeight < ToolboxConstant.TOOLBOX_WIDTH / ToolboxConstant.TOOLBOX_HEIGHT)
            {
               _loc1_ = _loc2_ = this._parentWidth / ToolboxConstant.TOOLBOX_WIDTH;
            }
            else
            {
               _loc1_ = _loc2_ = this._parentHeight / ToolboxConstant.TOOLBOX_HEIGHT;
            }
         }
         if(this._parentWidth > ToolboxConstant.MAX_PANEL_WIDTH || this._parentHeight > ToolboxConstant.MAX_PANEL_HEIGHT)
         {
            if(this._parentWidth / this._parentHeight < ToolboxConstant.TOOLBOX_WIDTH / ToolboxConstant.TOOLBOX_HEIGHT)
            {
               _loc1_ = _loc2_ = this._parentHeight / ToolboxConstant.MAX_PANEL_HEIGHT;
            }
            else
            {
               _loc1_ = _loc2_ = this._parentWidth / ToolboxConstant.MAX_PANEL_WIDTH;
            }
         }
         this.toolbox.scaleX = _loc1_;
         this.toolbox.scaleY = _loc2_;
         this.updateLayout();
      }
      
      private function onPlayerEvent(param1:PlayerControlEvent) : void {
         var _loc2_:String = null;
         var _loc3_:* = false;
         var _loc4_:* = false;
         var _loc5_:* = false;
         var _loc6_:* = NaN;
         var _loc7_:* = NaN;
         var _loc8_:* = NaN;
         switch(param1.type)
         {
            case PlayerControlEvent.SHOW_TOOLBOX:
               if(this._isShowing)
               {
                  this.restoreState();
                  this.closeToolbox();
               }
               else
               {
                  this.showToolbox();
               }
               break;
            case PlayerControlEvent.HIDE_TOOLBOX:
               this.hideToolbox();
               break;
            case PlayerControlEvent.BEFORE_SHOW_PANELS:
               _loc2_ = param1.data.panel;
               if(_loc2_ != PlayerConstant.PANEL_TOOLBOX)
               {
                  this.restoreState();
                  this.closeToolbox();
               }
               break;
            case PlayerControlEvent.COMMON_SET_CONTINUOUS:
               _loc3_ = param1.data.continuous;
               _loc4_ = this.toolbox.getCurrentData().isJump;
               this.toolbox.setFunctionValues(_loc3_,_loc4_);
               this.saveFunctionalData();
               break;
            case PlayerControlEvent.COMMON_SET_JUMP:
               _loc3_ = this.toolbox.getCurrentData().isContinuous;
               _loc4_ = param1.data.jump;
               this.toolbox.setFunctionValues(_loc3_,_loc4_);
               this.saveFunctionalData();
               break;
            case PlayerControlEvent.FULLSCREEN_CHANGED:
            case PlayerControlEvent.PLAYER_SIZE_CHANGED:
               _loc5_ = param1.data.fullscreen;
               _loc6_ = param1.data.width;
               _loc7_ = param1.data.height;
               _loc8_ = param1.data.cheight;
               this._parentWidth = _loc6_;
               this._parentHeight = _loc5_?_loc7_:_loc7_ - _loc8_;
               this.updateSize();
               break;
            case PlayerControlEvent.COMMON_RESET:
               this.hideToolbox();
               this.toolbox.reset();
               this.toolboxDataProxy.reset();
               break;
         }
      }
      
      private function onCoreEvent(param1:CoreEvent) : void {
         var _loc2_:String = null;
         switch(param1.type)
         {
            case CoreEvent.QUALITY_CHANGED:
               _loc2_ = param1.data.quality;
               this.toolboxDataProxy.toolboxData.quality = param1.data.quality;
               this.toolbox.selectQuality(_loc2_);
               break;
         }
      }
      
      private function onConductorEvent(param1:ConductorEvent) : void {
         var _loc2_:Array = null;
         var _loc3_:String = null;
         var _loc4_:String = null;
         var _loc5_:* = false;
         var _loc6_:* = false;
         var _loc7_:IDVDData = null;
         switch(param1.type)
         {
            case ConductorEvent.METADATA_UPDATED:
               _loc2_ = this._playerProxy.playerData.playlistTypeArr;
               _loc3_ = this._playerProxy.playerData.playlistCurrentQuality;
               _loc4_ = this._playerProxy.fsoProxy.getDefaultQuality();
               this.toolbox.setQualities(_loc2_);
               this.toolbox.setDefaultQuality(_loc4_);
               this.toolbox.selectQuality(_loc3_);
               _loc5_ = this._playerProxy.playerData.isContinuous;
               _loc6_ = this._playerProxy.playerData.isJump;
               this.toolbox.setFunctionValues(_loc5_,_loc6_);
               _loc7_ = this._playerProxy.playerData.dvdinfo;
               this.toolbox.setDVDValues(_loc7_);
               this.toolbox.setVoice(this._playerProxy.playerData.playlistCurrentLang);
               this.saveDVDData();
               this.saveFunctionalData();
               this.saveQualityData();
               break;
         }
      }
   }
}
